home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ActivationHelper.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  179 lines

  1. /*
  2.  * @(#)ActivationHelper.java    1.3 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import java.awt.*;
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.plaf.*;
  26.  
  27. /**
  28.  * @version @(#)ActivationHelper.java    1.0 1/21/98
  29.  * @author Symantec
  30.  * @author Levi Brown
  31.  * @author Craig Conner
  32.  */
  33.  
  34. class ActivationHelper extends JComponent
  35. {
  36.     protected Window                parentWindow;
  37.     protected MacAncestorListener    ancestorListener;
  38.     protected boolean                activated;
  39.     protected JComponent            c;
  40.     
  41.     public ActivationHelper()
  42.     {
  43.         parentWindow    = null;
  44.         activated        = false;
  45.         c                = null;
  46.         
  47.         //set up listener so we know when addNotify and removeNotify are called
  48.         ancestorListener = new MacAncestorListener();
  49.     }
  50.  
  51.     public ActivationHelper(JComponent c)
  52.     {
  53.         this();
  54.         setComponent(c);
  55.     }
  56.     
  57.     public void finalize()
  58.     {
  59.         setComponent(null);
  60.     }
  61.     
  62.     public void setComponent(JComponent c)
  63.     {
  64.         if(this.c != null)
  65.         {
  66.             this.c.removeAncestorListener(ancestorListener);
  67.         }
  68.  
  69.         this.c = c;
  70.  
  71.         if(this.c != null)
  72.         {
  73.             this.c.addAncestorListener(ancestorListener);
  74.         }
  75.     }
  76.     
  77.     public JComponent getComponent()
  78.     {
  79.         return c;
  80.     }
  81.     
  82.     public boolean isActivated()
  83.     {
  84.         return activated;
  85.     }
  86.  
  87.     protected void setActivated(boolean activated)
  88.     {
  89.         boolean oldValue = this.activated;
  90.         
  91.         this.activated = activated;
  92.  
  93.         firePropertyChange("activated", oldValue, activated);
  94.     }
  95.     
  96.     class MacAncestorListener implements com.sun.java.swing.event.AncestorListener
  97.     {
  98.         MacWindowAdapter macWindowAdapter;
  99.         
  100.         /**
  101.          * Called when the source or one of its ancestors is made visible
  102.          * either by setVisible(true) being called or by its being
  103.          * added to the component hierarchy.  The method is only called
  104.          * if the source has actually become visible.  For this to be true
  105.          * all its parents must be visible and it must be in a hierarchy
  106.          * rooted at a Window
  107.          */
  108.         public void ancestorAdded(com.sun.java.swing.event.AncestorEvent event)
  109.         {
  110.             Component parent = null;
  111.             int hHeight = 0, vWidth = 0;
  112.             
  113.             Object object = event.getSource();
  114.             if(object == c )
  115.                  parent = c.getParent();
  116.             
  117.                 while((parent != null) && (!(parent instanceof Window)))
  118.                     parent = parent.getParent();
  119.                     
  120.                 
  121.             if(parent != null)
  122.             {
  123.                 parentWindow = (Window)parent;
  124.                 
  125.                 macWindowAdapter = new MacWindowAdapter();
  126.                 parentWindow.addWindowListener(macWindowAdapter);
  127.             }
  128.         }
  129.     
  130.         /**
  131.          * Called when the source or one of its ancestors is made invisible
  132.          * either by setVisible(false) being called or by its being
  133.          * remove from the component hierarchy.  The method is only called
  134.          * if the source has actually become invisible.  For this to be true
  135.          * at least one of its parents must by invisible or it is not in
  136.          * a hierarchy rooted at a Window
  137.          */
  138.         public void ancestorRemoved(com.sun.java.swing.event.AncestorEvent event)
  139.         {
  140.             if(parentWindow != null)
  141.                 parentWindow.removeWindowListener(macWindowAdapter);
  142.         }
  143.     
  144.         /**
  145.          * Called when either the source or one of its ancestors is moved.
  146.          */
  147.         public void ancestorMoved(com.sun.java.swing.event.AncestorEvent event)
  148.         {
  149.         }
  150.     }
  151.     
  152.     class MacWindowAdapter extends java.awt.event.WindowAdapter
  153.     {
  154.         public void windowDeactivated(java.awt.event.WindowEvent event)
  155.         {
  156.             Object object = event.getSource();
  157.             if (object == parentWindow)
  158.                 ParentWindow_WindowDeactivated(event);
  159.         }
  160.  
  161.         public void windowActivated(java.awt.event.WindowEvent event)
  162.         {
  163.             Object object = event.getSource();
  164.             if (object == parentWindow)
  165.                 ParentWindow_WindowActivated(event);
  166.         }
  167.     }
  168.  
  169.     void ParentWindow_WindowActivated(java.awt.event.WindowEvent event)
  170.     {
  171.         setActivated(true);
  172.     }
  173.  
  174.     void ParentWindow_WindowDeactivated(java.awt.event.WindowEvent event)
  175.     {
  176.         setActivated(false);
  177.     }
  178. }
  179.